package com.example.packagenamegenerator;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private EditText vcodeEt;
	private Button genVcodeBtn;
	private TextView vcodeTv;
	public static final String alphabet[] = 
		{"a","b","c","d","e","f","g","h","i",
		 "j","k","l","m","n","o","p","q","r",
		 "s","t","u","v","w","x","y","z","kd",
		 "my","cy","ui","com","kl","ab","po","tr","hg"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initListener();
	}
	
	private void initView(){
		vcodeEt = (EditText) findViewById(R.id.vcode_et);
		genVcodeBtn = (Button) findViewById(R.id.gen_vcode_btn);
		vcodeTv = (TextView) findViewById(R.id.vcode_tv);
	}
	
	private void initListener(){
		genVcodeBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//获取当前文本 
				String vCode =vcodeEt.getText().toString().trim(); 
				String vCodeMd5 = StringUtils.md5Encoding(vCode);
				Log.i("jyh5", "~~~~vcode md5 is~~~"+vCodeMd5);
				String pkgName = pkgNameGenerator(vCodeMd5);
				vcodeTv.setText(pkgName);
				
				//测试
//				StringBuffer mList = new StringBuffer();
//				for(int k = 0; k < 1000; k++){
//					Log.e("jyh5", "~~~pkgName is ~~~"+pkgNameGenerator(vCodeMd5));
//					mList.append(pkgNameGenerator(vCodeMd5));
//				}
//				Log.e("jyh5", "~~~pkgName is ~~~"+mList.toString());
			}
		});
	}
	
	private String pkgNameGenerator(String vCodeMd5){
		StringBuffer pkgName = new StringBuffer();
		int k = 0;
		for(int i = 0; i + 4 <= vCodeMd5.length(); i += 4){
			k++;
			String subStr = vCodeMd5.substring(i, i+4);
			int letter = subStr.charAt(0)+subStr.charAt(1)+subStr.charAt(2)+subStr.charAt(3);
			letter = letter % 36;
			pkgName.append(alphabet[letter]);
			if(k % 2 == 0 && k < vCodeMd5.length()/4)
				pkgName.append('.');
		}
		return pkgName.toString();
	}
	
}
